package org.java.mathgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class HighScoreHandler {
    private Preferences preferences;

    public HighScoreHandler() {
        // ain't nothin' to it
        // gangsta rap made me do it
    }

    public class HighScore {
        private String name = "Michael_Scott";
        private int score = 0;
        public void setName(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public int getScore() {
            return score;
        }
    }

    protected Preferences getPrefs() {
        if(preferences==null){
            preferences = Gdx.app.getPreferences("mathgame-hs");
        }
        return preferences;
    }

    public void checkIfSet() {
        for (int i = 1; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (Integer.valueOf(getPrefs().getInteger("mode" + i + ".place" + (j + 1) + ".score")) == 0) {
                    setHighScore("John", 500, 1, 1);
                    setHighScore("Tywin", 400, 1, 2);
                    setHighScore("Stannis", 300, 1, 3);
                    setHighScore("Petyr", 200, 1, 4);

                    setHighScore("Walter", 500, 2, 1);
                    setHighScore("Gustavo", 400, 2, 2);
                    setHighScore("Hank", 300, 2, 3);
                    setHighScore("Jesse", 200, 2, 4);
                    return;
                }
            }
        }
    }

    public void setHighScore(String name, int score, int gameMode, int place) {
        getPrefs().putString("mode" + gameMode + ".place" + place + ".name", name);
        getPrefs().putInteger("mode" + gameMode + ".place" + place + ".score", score);
        getPrefs().flush();
    }

    public HighScore[] getHighScore(int gameMode) {
        HighScore[] highScore = new HighScore[4];
        for (int i = 0; i < 4; i++) {
            highScore[i] = new HighScore();
            highScore[i].setName(getPrefs().getString("mode" + gameMode + ".place" + (i + 1) + ".name"));
            highScore[i].setScore(getPrefs().getInteger("mode" + gameMode + ".place" + (i + 1) + ".score"));
        }
        return highScore;
    }

    public boolean isNewHighScore(int score, int gameMode) {
        for (int i = 0; i < 4; i++) {
            if (getHighScore(gameMode)[i].getScore() < score) {
                return true;
            }
        }
        return false;
    }

    public void checkHighScore(String name, int score, int gameMode) {
        int beaten = -1;
        counter:
        for (int i = 0; i < 4; i++) {
            if (getHighScore(gameMode)[i].getScore() < score) {
                beaten = i + 1;
                System.out.println(beaten);
                break counter;
            }
        }
        if (beaten != -1) {
            for (int i = 3; i >= beaten; i--) {
                System.out.println("replacing " + getHighScore(gameMode)[i].getName() + " with " + getHighScore(gameMode)[i-1].getName());
                setHighScore(getHighScore(gameMode)[i-1].getName(),
                        getHighScore(gameMode)[i-1].getScore(),
                        gameMode,
                        i+1);
                /*getHighScore(gameMode)[i+1].setName(getHighScore(gameMode)[i].getName());
                getHighScore(gameMode)[i+1].setScore(getHighScore(gameMode)[i].getScore());*/
            }
            setHighScore(name, score, gameMode, beaten);
            /*getHighScore(gameMode)[beaten].setName(name);
            getHighScore(gameMode)[beaten].setScore(score);*/
            //getPrefs().flush();
        }
    }
}